import openpyxl
from common1.all_path import *
import datetime
from shutil import copyfile
from common1.set_data import Setdata
from common1.read_config import Read_config

class DoExcel:

    def doex(self):
        read_con = Read_config().readconfig(path_config,'auto_test','auto_test') #{"testcase":"all"}或者{"testcase":[1,3]}
        datetimes = datetime.datetime.strftime(datetime.datetime.now(),'%Y%m%d%H%M')
        New_data=copyfile(path_test_data,path_test_data_path+f"\\yz_test_{datetimes}.xlsx")   #复制新表成新表
        setattr(Setdata,"New_excel",New_data)
        case_list = []
        wb=openpyxl.load_workbook(getattr(Setdata,'New_excel'))
        # sheet=wb['testcase']
        for keys in read_con:
            sheet = wb[keys]
            if read_con[keys]=='all':
                for rows in range(1,sheet.max_row):
                    dic={}
                    dic["caseId"]=sheet.cell(rows+1,1).value
                    dic["desc"]=sheet.cell(rows+1,2).value
                    dic["url"]=sheet.cell(rows+1,3).value
                    dic["http_method"]=sheet.cell(rows+1,4).value
                    dic["header"]=sheet.cell(rows+1,5).value
                    dic["Data"]=sheet.cell(rows+1,6).value
                    dic["expected"]=sheet.cell(rows+1,7).value
                    case_list.append(dic)
                return case_list
            else: #{"testcase":[1,3]}
                for rows in read_con[keys]:
                    dic={}
                    dic["caseId"]=sheet.cell(rows+1,1).value
                    dic["desc"]=sheet.cell(rows+1,2).value
                    dic["url"]=sheet.cell(rows+1,3).value
                    dic["http_method"]=sheet.cell(rows+1,4).value
                    dic["header"]=sheet.cell(rows+1,5).value
                    dic["Data"]=sheet.cell(rows+1,6).value
                    dic["expected"]=sheet.cell(rows+1,7).value
                    case_list.append(dic)
                return case_list


    def write_back(self,caseid,result,testresult):
        wb=openpyxl.load_workbook(getattr(Setdata,'New_excel'))
        sheet=wb['testcase']
        sheet.cell(caseid+1,8).value=result
        sheet.cell(caseid+1,9).value=testresult
        wb.save(getattr(Setdata,'New_excel'))


if __name__ == '__main__':
    res=DoExcel().doex()
    print(res)
    print(len(res))
    for i in res:
        print(i)
        print(type(i))
